package org.liujianlei;

import org.apache.commons.io.FileUtils;
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;

import static java.lang.Thread.sleep;

/**
 * @author tongchen
 * @create ${YEAR}-${MONTH}-${DAY} ${TIME}
 */

public class Main {


    public static void main(String[] args) throws InterruptedException, IOException {
       // test01();
       // test02();
        //test03();
        //test04();
        //test05();
        //test06();
        //test07();
       // test08();
        //test09();
        //test10();
       // test11();
       // test12();
        //page01();
       // page02();
        //page03();
        page04();
        //page05();
        //test13();
        //test14();
        //test15();
      //  test16();
        //test17();
        //test18();
    }
    private static void test18(){
        ChromeDriver chromeDriver = new ChromeDriver();
        //打开百度页面
        chromeDriver.get("https://www.baidu.com/");
        //获取资源
        chromeDriver.findElement(By.cssSelector("#kw")).sendKeys("软件测试");
        //输入内容
        chromeDriver.findElement( By.cssSelector("#su")).click();
        //点击搜搜
        //页面跳转
    }

    private static void test17() {
        WebDriver webDriver = new ChromeDriver();
        webDriver.get("https://www.baidu.com/");
        webDriver.findElement(By.cssSelector("#s-top-left > a:nth-child(5)v ")).click();
        //查看所有句柄
        Set<String> windowHandles = webDriver.getWindowHandles();
        String handle="";
        for(String target:windowHandles){
            handle=target;
        }
        //转化窗口
        webDriver.switchTo().window(handle);
        //点击
        webDriver.findElement(By.cssSelector("#__next > div > div.pageBase_pagebase__ipv9L.undefined > div.pageBase_pagebase-left__j_Sh3.pagebase-left-show > div.pageBase_pagebase-left-menus-outer__YeRG_ > div > div > div:nth-child(1) > div.left_page-leftinner-group-menu__vHmeL.left_page-leftinner-icon-hotvideo__J0lsK")).click();
    }

    private static void test16() {
        WebDriver webDriver = new ChromeDriver();
        webDriver.get("https://www.baidu.com/");
        webDriver.findElement(By.cssSelector("#s-top-left > a:nth-child(1)")).click();
        webDriver.quit();
    }

    private static void test15() throws IOException {
        //创建webdriver
        ChromeDriver webDriver = new ChromeDriver();
        webDriver.get("https://www.baidu.com");
        //转化对象
        File file = ((TakesScreenshot) webDriver).getScreenshotAs(OutputType.FILE);
        //截图文件转存
       String fileName="D:\\my.png";
        FileUtils.copyFile(file, new File(fileName));

    }

    private static void test14() throws InterruptedException {
        WebDriver webDriver = new ChromeDriver();
        webDriver.get("https://www.baidu.com/");
        webDriver.findElement(By.cssSelector("#kw")).sendKeys("ruanjian");
        webDriver.findElement(By.cssSelector("#su")).click();
        sleep(3000);
        ((JavascriptExecutor)webDriver).executeScript("document.documentElement.scrollTop=10000");
    }

    private static void test13() {
        //创建driver对象
        ChromeDriver chromeDriver = new ChromeDriver();
        chromeDriver.get("https://www.baidu.com");
        WebDriverWait webDriverWait = new WebDriverWait(chromeDriver, 3);
        webDriverWait.until(ExpectedConditions.titleIs("百度一下，你就知道！"));
    }

    private static void page05() {
        //创建driver
        ChromeDriver chromeDriver = new ChromeDriver();
        //打开网页
        chromeDriver.get("http://localhost:63342/javacode/softwaretest/page/test05.html?_ijt=5ojv0p7i4a0tri5nnnpn070oju&_ij_reload=RELOAD_ON_SAVE");
        //查找元素并点击
        chromeDriver.findElement(By.cssSelector("input")).sendKeys("C:\\local\\device_capability.ini");

    }

    private static void page04() throws InterruptedException {
        //创建driver
        ChromeDriver driver = new ChromeDriver();
        //打开网页
        driver.get("http://localhost:63342/javacode/softwaretest/page/test04.html?_ijt=gfogq5h5uajcrbcblenlr54n81&_ij_reload=RELOAD_ON_SAVE");
        //点击按钮
        driver.findElement(By.cssSelector("body > button")).click();
        //Thread.sleep(3000);
        //切换弹窗
        driver.switchTo().alert().sendKeys("NIHO");
        driver.switchTo().alert().dismiss();
        driver.switchTo().alert().accept();
        //是否操作


    }

    private static void page03() {
        //创建driver
        ChromeDriver chromeDriver = new ChromeDriver();
        //打开网页
        chromeDriver.get("http://localhost:63342/javacode/softwaretest/page/test03.html?_ijt=5ihdqn6t7nc2bchk83bc31pjem&_ij_reload=RELOAD_ON_SAVE");
        //操作下拉框
        Select select = new Select(chromeDriver.findElement(By.cssSelector("#ShippingMethod")));
        select.selectByValue("7.45");
        select.selectByIndex(2);
    }

    private static void page02() {
        //创建驱动
        ChromeDriver chromeDriver = new ChromeDriver();
        //打开浏览器
        chromeDriver.get("http://localhost:63342/javacode/softwaretest/page/test02.html?_ijt=dabgaqjv4h84gtl6ugla25fkbb&_ij_reload=RELOAD_ON_SAVE");
        //转化
        chromeDriver.switchTo().frame("f1");
        //查找元素并点击
        chromeDriver.findElement(By.cssSelector("body > div > div > a")).click();
    }

    private static void page01() {
        //创建驱动
        ChromeDriver driver = new ChromeDriver();
        //打开网页
        driver.get("http://localhost:63342/javacode/softwaretest/page/test01.html?_ijt=ausofes2kq9otjcj826rd85v1m&_ij_reload=RELOAD_ON_SAVE");
        //查找元素
        List<WebElement> input = driver.findElements(By.cssSelector("input"));
        //遍历并点击元素
        for(WebElement element:input){
            if(element.getAttribute("type").equals("checkbox")){
                element.click();
            }
        }
    }

    private static void test12() {
        ChromeOptions chromeOptions = new ChromeOptions();
        chromeOptions.addArguments("--remote-allow-origins=*");
        ChromeDriver chromeDriver = new ChromeDriver(chromeOptions);
        chromeDriver.get("https://www.baidu.com");
        WebElement element = chromeDriver.findElement(By.cssSelector("#s-top-left > a:nth-child(1)"));
        Actions actions = new Actions(chromeDriver);
        actions.contextClick(element).perform();
    }

    private static void test11() throws InterruptedException {
        ChromeOptions chromeOptions = new ChromeOptions();
        chromeOptions.addArguments("--remote-allow-origins=*");
        ChromeDriver chromeDriver = new ChromeDriver(chromeOptions);
        chromeDriver.get("https://www.baidu.com");
        chromeDriver.findElement(By.cssSelector("#kw")).sendKeys("测试");
        chromeDriver.findElement(By.cssSelector("#su")).click();
        sleep(1000);
        chromeDriver.findElement(By.cssSelector("#kw")).sendKeys(Keys.CONTROL +"A");
        sleep(1000);
        chromeDriver.findElement(By.cssSelector("#kw")).sendKeys(Keys.CONTROL+"X");
        sleep(1000);
        chromeDriver.findElement(By.cssSelector("#kw")).sendKeys(Keys.CONTROL+"V");
    }

    private static void test10() throws InterruptedException {
        //创建options对象
        ChromeOptions options = new ChromeOptions();
        //允许接收所哟参数
        options.addArguments("--remote-allow-origins=*");
        ChromeDriver chromeDriver = new ChromeDriver(options);
        //打开网页
        chromeDriver.get("https://www.baidu.com");
        //搜索东西
        chromeDriver.findElement(By.cssSelector("#kw")).sendKeys("测试");
        chromeDriver.findElement(By.cssSelector("#su")).click();
        sleep(1000);
        chromeDriver.navigate().back();
        //前进操作
        chromeDriver.navigate().forward();
        chromeDriver.navigate().refresh();
    }

    private static void test09() throws InterruptedException {
        //创建options对象
        ChromeOptions chromeOptions = new ChromeOptions();
        //允许接收所有请求
        chromeOptions.addArguments("--remote-allow-origins=*");
        //创建驱动
        ChromeDriver chromeDriver = new ChromeDriver(chromeOptions);
        //打开网页
        chromeDriver.get("https://www.baidu.com");
        //修改窗口大小
        chromeDriver.manage().window().maximize();
        //休眠
        sleep(1000);
        //修改为全屏
        chromeDriver.manage().window().fullscreen();
        //休眠
        sleep(1000);
        //修改为指定大小
        chromeDriver.manage().window().setSize(new Dimension(500, 500));
    }

    private static void test08() {
        //获取options
        ChromeOptions options = new ChromeOptions();
        //允许所有请求
        options.addArguments("--remote-allow-origins=*");
        //获取驱动
        ChromeDriver driver = new ChromeDriver();
        //打开网页
        driver.get("https://www.baidu.com");
        //获取当前网页的title
        String title = driver.getTitle();
        //打印title
        System.out.println(title);
    }

    private static void test07() {
        //创建options对象，允许设置参数
        ChromeOptions chromeOptions = new ChromeOptions();
        //允许所有请求
        chromeOptions.addArguments("--remote-allow-origins=*");
        //创建驱动
        ChromeDriver chromeDriver = new ChromeDriver(chromeOptions);
        //打开网页
        chromeDriver.get("https://www.baidu.com");
        //获取当前url
        String currentUrl = chromeDriver.getCurrentUrl();
        System.out.println(currentUrl);
    }

    private static void test06() {
        //创建options对象，允许设置请求参数
        ChromeOptions options = new ChromeOptions();
        //允许所有请求
        options.addArguments("--remote-allow-origins=*");
        //创建驱动
        ChromeDriver chromeDriver = new ChromeDriver(options);
        //打开网页
        chromeDriver.get("https://www.baidu.com");
        //输入并搜索
        chromeDriver.findElement(By.cssSelector("#kw")).sendKeys("软件测试");
        chromeDriver.findElement(By.cssSelector("#su")).click();
        //智能等待
        chromeDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        //清空
        chromeDriver.findElement(By.cssSelector("#kw")).clear();
    }

    private static void test05() {
        //创建options对象，允许设置请求参数
        ChromeOptions options = new ChromeOptions();
        //允许所有请求
        options.addArguments("--remote-allow-origins=*");
        //创建驱动
        ChromeDriver webDriver = new ChromeDriver(options);
        //打开网页
        webDriver.get("https://www.baidu.com");
        //输入并搜索
        webDriver.findElement(By.cssSelector("#kw")).sendKeys("软件测试");
        //清空搜索框的内容
        webDriver.findElement(By.cssSelector("#su")).click();
        webDriver.findElement(By.cssSelector("#kw")).clear();
    }

    private static void test04() {
        // 创建options对象，允许设置请求参数
        ChromeOptions options = new ChromeOptions();
        //允许所有请求
        options.addArguments("--remote-allow-origins=*");
        //创建驱动
        ChromeDriver driver = new ChromeDriver(options);
        //打开网页
        driver.get("https://www.baidu.com");
        //点击
        //driver.findElement(By.cssSelector("#s-top-left > a:nth-child(3)")).submit();(报错)
        driver.findElement(By.cssSelector("#s-top-left > a:nth-child(3)")).click();
    }

    private static void test03() throws InterruptedException {
        //创建options对象，设置请求参数
        ChromeOptions chromeOptions = new ChromeOptions();
        //允许所有请求
        chromeOptions.addArguments("--remote-allow-origins=*");
        //创建驱动
        ChromeDriver chromeDriver = new ChromeDriver(chromeOptions);
        //打开网页
        chromeDriver.get("https://www.baidu.com");
        //绑定元素
//        chromeDriver.findElement(By.cssSelector("#kw")).sendKeys("热搜");
        System.out.println("1111");
        //获取元素
        String text = chromeDriver.findElement(By.cssSelector("#su")).getAttribute("value");
        System.out.println("2222");
        System.out.println(text);
//        Thread.sleep(3000);
//        chromeDriver.quit();
    }

    private static void test02() throws InterruptedException {
        //创建options对象，用来设置请求参数
        ChromeOptions chromeOptions = new ChromeOptions();
        //允许所有请求
        chromeOptions.addArguments("--remote-allow-origins=*");
        //创建驱动
        ChromeDriver chromeDriver = new ChromeDriver(chromeOptions);
        //打开网页
        chromeDriver.get("https://www.baidu.com");
        //绑定元素
        chromeDriver.findElement(By.cssSelector("#kw")).sendKeys("软件测试");
        //点击元素、
        chromeDriver.findElement(By.cssSelector("#su")).click();
        //休眠四秒
        sleep(4000);
        //查看结果
        List<WebElement> em = chromeDriver.findElements(By.cssSelector("em"));
        if(em.size()==0){
            System.out.println("测试失败");
        }else{
            System.out.println("测试成功");
        }
        chromeDriver.quit();
    }

    private static void test01() {
        //创建options对象,用来给请求设置参数
        ChromeOptions options = new ChromeOptions();
        //允许所有请求
        options.addArguments("--remote-allow-origins=*");
        //创建驱动
        ChromeDriver webDriver = new ChromeDriver(options);
        //打开百度网页
        webDriver.get("https://www.baidu.com");
        //关闭网页
        webDriver.close();
    }
}